package com.lxd.filter.Impl;

import com.lxd.common.constants.CacheConstant;
import com.lxd.common.constants.RabbitMQConstants;
import com.lxd.common.constants.StrategyConstant;
import com.lxd.common.model.StandardSubmit;
import com.lxd.utils.MobileOperatorUtil;
import com.lxd.feign.cacheClient;
import com.lxd.filter.CheckFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Dongdong
 * @注释  号段补全
 */
@Service("phase")
@Slf4j
public class PhaseStrategyFilter implements CheckFilter {

    @Autowired
    private cacheClient client;
    @Autowired
    private MobileOperatorUtil util;
    @Autowired
    private RabbitTemplate mq;


    @Override
    public void check(StandardSubmit submit) {
        //1.截取前7位手机号
        String mobile = submit.getMobile().substring(StrategyConstant.MOBILE_START, StrategyConstant.MOBILE_END);
        log.info("【策略模块-号段补全】 客户手机号前7位={}",mobile);
        //redis查询手机号的归属地及运营商信息
        String mobileInfo = client.getString(CacheConstant.PHASE+mobile);
        //redis mysql中不存在该手机号的信息
        if (StringUtils.isEmpty(mobileInfo)){
             log.info("【策略模块-号段补全】 客户不存在，即将调用三方查询");
            //2.调用三方查询
            mobileInfo=util.selectAreaAndType(mobile);

            if (StringUtils.isEmpty(mobileInfo)){
                //三方查询结果为空， 将信息写成未知
                mobileInfo = StrategyConstant.UNKNOWN;
            }
            //3.将消息发送到mq，并且同步到redis和mysql
            mq.convertAndSend(RabbitMQConstants.MOBILE_AREA_OPERATOR ,submit.getMobile());
        }

        //4.将归属地及运营商信息封装到submit中
        log.info("【策略模块-号段补全】查到的信息={}",mobileInfo);
        String[] area=mobileInfo.split(",");
        log.info("【策略模块-号段补全】 客户的area={},地区type={}",area[0],area[1]);
        submit.setArea(area[0]);
        submit.setMobile_type(area[1]);








    }
}
